package com.example.tms.controller;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.example.tms.bean.*;
import com.example.tms.mapper.*;
import com.example.tms.tools.GlobalParam;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 导入excel数据
 */
@RestController
public class ExcelUtil {

    private RoadMapper roadMapper;
    private AreaMapper areaMapper;
    private CarMapper carMapper;
    private DriverMapper driverMapper;
    private DeptMapper deptMapper;

    @Autowired
    public void setRoadMapper(RoadMapper roadMapper){
        this.roadMapper = roadMapper;
    }
    @Autowired
    public void setAreaMapper(AreaMapper areaMapper){
        this.areaMapper = areaMapper;
    }
    @Autowired
    public void setCarMapper(CarMapper carMapper){
        this.carMapper = carMapper;
    }
    @Autowired
    public void setDriverMapper(DriverMapper driverMapper){
        this.driverMapper = driverMapper;
    }
    @Autowired
    public void setDeptMapper(DeptMapper deptMapper){
        this.deptMapper = deptMapper;
    }

    //导入线路信息
    @RequestMapping(value = "excelXl",method = RequestMethod.POST)
    public ReturnInfo excelUtilXl(){
        String path = "C:\\Users\\asus\\Desktop\\work\\TMS智运宝\\xl.xlsx";
        try {
            List<List<String>> result = new ExcelUtil().readXlsx(path);
            System.out.println(result.size());
            for (List<String> model : result) {
                RoadInfo roadInfo = new RoadInfo();
                roadInfo.setXlmc(model.get(0));//线路名称
                roadInfo.setXlbm(model.get(1));//线路编码
                roadInfo.setKhid("4028e38161f419860161f41ab9da0000");
                AreaInfo areaInfo = areaMapper.findByQymc(model.get(3));
                roadInfo.setQyid(areaInfo.getQyid());
                //model.get(3);区域名称
                roadInfo.setXllx(model.get(4));//线路类型
                roadInfo.setLeixing(model.get(5));//班次类型
                roadInfo.setGls(model.get(6));//公里数
                roadInfo.setXlid(UUID.randomUUID().toString().replaceAll("-",""));
                roadInfo.setLasttime(GlobalParam.getTime());
                roadInfo.setSfcz(true);
                roadInfo.setJtdm("{}");
                roadMapper.save(roadInfo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ReturnInfo.info1("成功");
    }

    //导入车辆信息
    @RequestMapping(value = "excelCl",method = RequestMethod.POST)
    public ReturnInfo excelUtilCl(){
        String path = "C:\\Users\\asus\\Desktop\\work\\TMS智运宝\\cl.xlsx";
        try {
            List<List<String>> result = new ExcelUtil().readXlsx(path);
            System.out.println(result.size());
            for (List<String> model : result) {
                CarInfo carInfo = new CarInfo();
                carInfo.setCph(model.get(0));
                if(model.get(1).equals("否")){
                    carInfo.setSfwjc("否");
                }else {
                    carInfo.setSfwjc("是");
                }
                carInfo.setClxh(model.get(2));
                carInfo.setCjh(model.get(3));
                carInfo.setFdjh(model.get(4));
                carInfo.setDjrq(model.get(5));
                carInfo.setGcorjk(model.get(6));
                carInfo.setCsys(model.get(7));
                carInfo.setPlorgl(model.get(8));
                carInfo.setZzcs(model.get(9));
                if(!model.get(10).equals("**")){
                    String[] a = model.get(10).split("\\*");
                    carInfo.setWlccc(a[0]);
                    carInfo.setWlcck(a[1]);
                    carInfo.setWlccg(a[2]);
                }
                if(model.get(0).equals("浙A817FV")){
                    carInfo.setWlccc("5880");
                }
                carInfo.setYyzh(model.get(11));
                if(!model.get(12).equals("**")){
                    carInfo.setZgcch(model.get(12));
                }else {
                    carInfo.setZgcch("");
                }
                carInfo.setClid(UUID.randomUUID().toString().replaceAll("-",""));
                carInfo.setYkid("");
                carInfo.setSjid("");
                carInfo.setSffp("未分配");
                carInfo.setClzt("可用");
                carInfo.setLasttime(GlobalParam.getTime());
                carInfo.setSfcz(true);
                carMapper.save(carInfo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ReturnInfo.info1("成功");
    }

    //导入司机信息
    @RequestMapping(value = "excelSj",method = RequestMethod.POST)
    public ReturnInfo excelUtilSj(){
        String path = "C:\\Users\\asus\\Desktop\\work\\TMS智运宝\\sj.xlsx";
        try {
            List<List<String>> result = new ExcelUtil().readXlsx(path);
            System.out.println(result.size());
            for (List<String> model : result) {
                /*
                车辆分配司机
                 */
                if(!model.get(11).equals("")&&model.get(11)!=null&&!model.get(0).equals("赵建")&&!model.get(0).equals("刘涛")&&!model.get(0).equals("林发印")&&!model.get(0).equals("赵富强")&&!model.get(0).equals("范志刚")&&!model.get(0).equals("王磊")&&!model.get(0).equals("刘辉")&&!model.get(0).equals("张良超")&&!model.get(0).equals("李志华")&&!model.get(0).equals("刘磊")&&!model.get(0).equals("张勇")){
                    CarInfo carInfo = carMapper.findByCph(model.get(11));
                    DriverInfo driverInfo = driverMapper.findByJsyxm(model.get(0));
                    if(driverInfo!=null&&carInfo!=null){
                        carMapper.updateCarSj(carInfo.getClid(),driverInfo.getSjid());
                    }
                }
                /*
                导入司机基本信息
                 */
                DriverInfo driverInfo = new DriverInfo();
                driverInfo.setJsyxm(model.get(0));
                if(!model.get(1).equals("无")){
                    DeptInfo deptInfo = deptMapper.findByName(model.get(1));
                    driverInfo.setBumenid(deptInfo.getBumenid());
                }else {
                    driverInfo.setBumenid("");
                }
                driverInfo.setJsysj(model.get(2));
                driverInfo.setSfzh(model.get(3));
                driverInfo.setSfzszd(model.get(4));
                driverInfo.setJszh(model.get(5));
                driverInfo.setJszclrq(model.get(6).replaceAll("\\.","-"));
                driverInfo.setJszyxqz(model.get(7).replaceAll("\\.","-"));
                driverInfo.setCyzgzh(model.get(8));
                driverInfo.setCyzgzclrq(model.get(9).replaceAll("\\.","-"));
                driverInfo.setCyzgzyxqz(model.get(10).replaceAll("\\.","-"));
                CarInfo carInfo = carMapper.findByCph(model.get(11));
                if(carInfo!=null){
                    driverInfo.setClid(carInfo.getClid());
                }
                if(model.get(12).equals("请假")){
                    driverInfo.setZzzt("离职");
                }else {
                    driverInfo.setZzzt(model.get(12));
                }
                driverInfo.setJjlxrm("{}");
                driverInfo.setDah("");
                driverInfo.setSfzfyj("");
                driverInfo.setJkzmzp("");
                driverInfo.setLasttime(GlobalParam.getTime());
                driverInfo.setSfcz(true);
                driverMapper.save(driverInfo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ReturnInfo.info1("成功");
    }

    private List<List<String>> readXlsx(String path) throws Exception {
        InputStream is = new FileInputStream(path);
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);
        List<List<String>> result = new ArrayList<>();
        // 循环每一页，并处理当前循环页
        for (Sheet xssfSheet : xssfWorkbook) {
            if (xssfSheet == null) {
                continue;
            }
            // 处理当前页，循环读取每一行
            for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
                Row xssfRow = xssfSheet.getRow(rowNum);
                int minColIx = xssfRow.getFirstCellNum();
                int maxColIx = xssfRow.getLastCellNum();
                List<String> rowList = new ArrayList<>();
                for (int colIx = minColIx; colIx < maxColIx; colIx++) {
                    Cell cell = xssfRow.getCell(colIx);
                    if (cell == null) {
                        continue;
                    }
                    rowList.add(cell.toString());
                }
                result.add(rowList);
            }
        }
        return result;
    }

}
